#include "table_item.h"

void UserInfo::json2data(const QJsonObject &data,UserInfo &user_data)
{
    user_data.user_id = data.value("id").toInt();
    user_data.name = data.value("name").toString();
}

void TableItem::json2data(const QJsonObject &data,TableItem &table_item_data)
{
    table_item_data.id = data.value("id").toInt();
    table_item_data.name = data.value("name").toString();
    table_item_data.status = data.value("status").toInt();
    for(QJsonValueRef c:data["users"].toArray())
    {
        QJsonObject json_user = c.toObject();
        UserInfo info;

        if(c.isNull())
        {
            info.user_id=0;
            info.name = "";
        }
        else
        {
            UserInfo::json2data(json_user,info);
        }
        table_item_data.users.append(info);
    }
}

void TableAction::json2data(const QJsonObject &data,TableAction &action_data)
{
    if(data.contains("action"))
    {
        action_data.action = data["action"].toInt();
    }
    else
    {
        action_data.action=0;
    }

    if(data.contains("cards"))
    {
        for(QJsonValueRef c:data["cards"].toArray())
        {
            action_data.cards.append(c.toInt());
        }
    }
    action_data.id = data["id"].toInt();
    action_data.index = data["index"].toInt();
}

void TableUser::json2data(const QJsonObject &data,TableUser &table_user_data)
{
    QJsonArray j_cards = data["cards"].toArray();
    for(QJsonValueRef card:j_cards)
    {
        table_user_data.cards.append(card.toInt());
    }
    table_user_data.name = data["name"].toString();
    table_user_data.prepared = data["prepared"].toBool();
    table_user_data.role = data["role"].toInt();
    table_user_data.user_id = data["user_id"].toInt();
    table_user_data.status = data["status"].toInt();
    QJsonArray j_played_cards = data["played_cards"].toArray();
    for(QJsonValueRef card:j_played_cards)
    {
        table_user_data.played_cards.append(card.toInt());
    }
    table_user_data.user_id = data["user_id"].toInt();

}

void TabaleData::json2data(const QJsonObject &data,TabaleData &table_data)
{
    {
        QJsonObject j_action_params = data.value("action_params").toObject();
        {

            TableAction::json2data(j_action_params,table_data.action_params);
        }
        table_data.action_result = data.value("action_result").toInt();
        QJsonObject j_action_user = data.value("action_user").toObject();
        {
            UserInfo::json2data(j_action_user,table_data.action_user);

        }
        QJsonArray j_cards3 =  data.value("cards3").toArray();
        for(QJsonValueRef c:j_cards3)
        {
            table_data.cards3.append(c.toInt());
        }
        QJsonArray j_current_play_max_cards =  data.value("current_play_max_cards").toArray();
        for(QJsonValueRef c:j_current_play_max_cards)
        {
            table_data.current_play_max_cards.append(c.toInt());
        }

        table_data.current_play_max_cards_type = data.value("current_play_max_cards_type").toInt();
        table_data.id = data.value("id").toInt();
        table_data.landlord_index = data.value("landlord_index").toInt();
        table_data.play_cards_index = data.value("play_cards_index").toInt();
        table_data.prev_play_max_card_index = data.value("prev_play_max_card_index").toInt();
        table_data.step = data.value("step").toInt();

        QJsonArray j_users =  data.value("users").toArray();
        for(QJsonValueRef c:j_users)
        {
            TableUser u;
            if(c.isNull())
            {
                u.user_id = 0;
                u.name = "";
            }
            else
            {
                QJsonObject j_user = c.toObject();
                {
                    TableUser::json2data(j_user,u);
                }
            }
            table_data.users.append(u);
        }

        table_data.winner_index = data["winner_index"].toInt();
    }
}
void ChatContentData:: json2data(const QJsonObject &data,ChatContentData &a_chat_content_data)
{
    a_chat_content_data.message = data["message"].toString();
    a_chat_content_data.time = data["time"].toInt();
}

void ChatData::json2data(const QJsonObject &data,ChatData &cdata)
{
    cdata.user_id = data["user_id"].toInt();
    cdata.name = data["name"].toString();
    QJsonObject j_content = data["content"].toObject();
    {
        ChatContentData::json2data(j_content,cdata.content);
    }
}

void ChatTableData::json2data(const QJsonObject &data,ChatTableData &cdata)
{
    cdata.user_id = data["user_id"].toInt();
    cdata.name = data["name"].toString();
    QJsonObject j_content = data["content"].toObject();
    {
        ChatContentData::json2data(j_content,cdata.content);
    }
    cdata.table_id = data["table_id"].toInt();

}
void PersonNotifyhData::json2data(const QJsonObject &data,PersonNotifyhData &cdata){
    TableAction::json2data(data["action_params"].toObject(),cdata.action_params);
    cdata.action_result = data["action_result"].toInt();
    UserInfo::json2data(data["action_user"].toObject(),cdata.action_user);
}
